package com.atguigu.tingshu.user.ItemTypeStrategy;

import java.util.List;

import com.atguigu.tingshu.album.AlbumFeignClient;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.user.UserPaidTrack;
import com.atguigu.tingshu.user.mapper.UserPaidTrackMapper;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("1002")
public class TrackTypeStrategyImpl implements ItemTypeStrategy {
    @Autowired
    UserPaidTrackMapper userpaidTrackMapper;
    @Autowired
    AlbumFeignClient albumFeignClient;

    @Override
    public void SavePaidRecord(UserPaidRecordVo userPaidRecordVo) {
        String orderNo = userPaidRecordVo.getOrderNo();
        Long userId = userPaidRecordVo.getUserId();
        String itemType = userPaidRecordVo.getItemType();
        List<Long> itemIdList = userPaidRecordVo.getItemIdList();
        QueryWrapper<UserPaidTrack> trackInfoQueryWrapper = new QueryWrapper<>();
        trackInfoQueryWrapper.eq("order_no", userPaidRecordVo.getOrderNo());
        Long l = userpaidTrackMapper.selectCount(trackInfoQueryWrapper);
        if (l > 0) {
            return;
        }
        for (Long trackId : itemIdList) {
            AlbumInfo data = albumFeignClient.getAlbumInfobytrackId(trackId).getData();
            Long albumId = data.getId();
            UserPaidTrack userPaidTrack = new UserPaidTrack();
            userPaidTrack.setUserId(userId);

            userPaidTrack.setAlbumId(albumId);
            userPaidTrack.setOrderNo(orderNo);
            userPaidTrack.setTrackId(trackId);
            userpaidTrackMapper.insert(userPaidTrack);
        }


    }
}
